import * as types from './mutation-types'
import { allApi } from '../api/api'
import Storage from '../utils/storage'

export const showLoading = ({ commit, state }, title) => commit(types.SHOW_LOADING, title)
export const hideLoading = ({ commit, state }) => {
  // setTimeout(() => {
    commit(types.HIDE_LOADING)
  // },0)
}

export const setMsgTip = ({ commit, state },msgObj) => {
    let timer = setTimeout(() => {
        commit(types.HIDE_MSGTIP)
    },3000)
    msgObj.timer = timer
    commit(types.SET_MSGTIP, msgObj)
}
export const setListInfo = ({ commit, state },listObj) => {
    commit(types.SET_LISTINFO, listObj)
}
export const setMsgTipStyle = ({ commit, state },styleObj) => {
    commit(types.SET_MSGTIPSTYLE, styleObj)
}
//qiancang
// export const setMenuList = ({ commit, state },arr) => {
    
    
//     if(!arr){
//         arr = Storage.get(`51SjdAdmin_menuArr`) || []
//     }
//     let newarr = []
//     let keyArr=[];
//     let valueArr=[];
//     _.each(arr,(value,index)=>{
//         keyArr.push(value.mid)
       
        
//     })
//     let sortArr=keyArr.sort(compare);
//     _.each(arr,(value,index)=>{
//        if(value.mid==sortArr[index]){
//             valueArr.push(value.menu)
//        }
        
//     })
//     console.log(valueArr)
//     function compare(value1, currentValue){
//         return value1 - currentValue;
//     }
//      _.each(valueArr,(value,index)=>{
        
//         if(value == "channel"){
//             console.log(localStorage.getItem("roles"))
//             if(localStorage.getItem("roles")=="channel_operator"){
//                     newarr.push([{
//                         text:'渠道查询',
//                         router:'channel-inquiry',
//                         opened:false,
//                         subRouters:[/*{
//                             name:'channel-management',
//                             text:'渠道商管理'
//                         },*/{
//                             name:'channel-inquiry',
//                             text:'渠道查询'
//                         }],
//                     }])
//             }else{
//                 newarr.push([{
//                 text:'渠道管理',
//                 router:'channel',
//                 opened:false,
//                 subRouters:[{
//                     name:'channel-management',
//                     text:'渠道商管理'
//                 },{
//                     name:'channel-inquiry',
//                     text:'渠道查询'
//                 }],
//             }])
//             }
            
//         }
//         if(value == "dashboard"){ //
             
//             newarr.push([{
//                 text:'仪表盘',
//                 router:'dashboard',
//                 opened:false,
//                  subRouters:[{
//                     name:'dash-board',
//                     text:'仪表盘'
//                 }],
//             }])
//             newarr.push([{
//                 text:'财务统计报表',
//                 router:'financial',
//                 opened:false,
//                  subRouters:[{
//                     name:'financial',
//                     text:'财务统计报表'
//                 }],
//             }])
//             /*newarr.push([{
//                 text:'应收账款统计',
//                 router:'receipt',
//                 opened:false,
//                  subRouters:[{
//                     name:'receipt',
//                     text:'应收账款统计'
//                 }],
//             }])*/
//             return
//         }
//       if(value == 'system'){
//             newarr.push([{
//                 text:'通用管理',
//                 router:'system',
//                 opened:false,
//                 subRouters:[{
//                     name:'system-user',
//                     text:'员工账号管理'
//                 },{
//                     name:'system-product-config',
//                     text:'产品配置'
//                 },{
//                     name:'system-risk-config',
//                     text:'风控条件配置'
//                 }/*,{
//                     name:'system-icloud-show',
//                     text:'iCloud信息配置'
//                 },{
//                     name:'system-app-config',
//                     text:'代理商配置'
//                 }*/],
//             }])
//             return
//         }
//         /*if(value == 'icould'){
//             newarr.push([{
//                 text:'iCloud账号管理',
//                 router:'icloud',
//                 opened:false,
//                 subRouters:[{
//                     name:'icloud-index',
//                     text:'iCloud账号管理'
//                 }],
//             }])
//             return
//         }*/
//         if(value == 'auth'){
//             newarr.push([{
//                 text:'认证申请管理',
//                 router:'auth-apply',
//                 opened:false,
//                 subRouters:[{
//                     name:'auth-apply-wait',
//                     text:'待审核'
//                 },{
//                     name:'auth-apply-history',
//                     text:'历史审核'
//                 }],
//             }])
//             return
//         }
//         if(value == 'loan'){
//             newarr.push([{
//                 text:'借款申请管理',
//                 router:'loan-apply',
//                 opened:false,
//                 subRouters:[{
//                     name:'loan-apply-wait',
//                     text:'待审核'
//                 },{
//                     name:'loan-apply-history',
//                     text:'历史审核'
//                 }/*,{
//                     name:'finance-out',
//                     query:{
//                         type:'other'
//                     },
//                     text:'放款记录查询'
//                 }*/],
//             }])
//             return
//         }
//         if(value == 'lend'){
//             if(localStorage.getItem("roles")=="system_admin"){
//                 newarr.push([{
//                     text:'财务管理',
//                     router:'finance',
//                     opened:false,
//                     subRouters:[{
//                         name:'finance-wait',
//                         text:'待放款记录'
//                     }/*,{
//                         name:'finance-reject',
//                         text:'拒绝放款记录'
//                     }*//*,{
//                         name:'finance-ing',
//                         text:'放款中记录'
//                     }*/,{
//                         name:'finance-out',
//                         text:'放款记录'
//                     }/*,{
//                         name:'finance-in',
//                         text:'还款记录'
//                     }*/,{
//                     name:'receipt',
//                     text:'应收账款统计'
//                     },{
//                         name:'finance-extend',
//                         text:'展期记录'
//                     }],
//                 }])
//             }else{
//                 newarr.push([{
//                     text:'财务管理',
//                     router:'finance',
//                     opened:false,
//                     subRouters:[{
//                         name:'finance-wait',
//                         text:'待放款记录'
//                     }/*,{
//                         name:'finance-reject',
//                         text:'拒绝放款记录'
//                     }*//*,{
//                         name:'finance-ing',
//                         text:'放款中记录'
//                     }*/,{
//                         name:'finance-out',
//                         text:'放款记录'
//                     },{
//                         name:'finance-in',
//                         text:'还款记录'
//                     },{
//                         name:'finance-extend',
//                         text:'展期记录'
//                     }],
//                 }])
//             }
            
//             return
//         }
//         if(value == 'repay'){
//             newarr.push([{
//                 text:'贷后管理',
//                 router:'after-loan',
//                 opened:false,
//                 subRouters:[{
//                     name:'after-loan-wait',
//                     text:'案件催收' //待催收
//                 },{
//                     name:'after-loan-history',
//                     text:'个人对账列表' //历史催收
//                 },/*{
//                     name:'after-loan-error',
//                     text:'异常催收'
//                 },*//*,{
//                     name:'finance-out',
//                     query:{
//                         type:'other'
//                     },
//                     text:'放款记录查询'
//                 }*/{
//                     name:'after-loan-allot-index',
//                     query:{
//                         routerType:'all'
//                     },
//                     text:'案件管理'
//                 }],
//             }])
//             return
//         }
//        /* if(value == 'repay_allot'){
//             newarr.push([{
//                 text:'贷后数据看板', //贷后分配
//                 router:'after-loan-allot',
//                 opened:false,
//                 subRouters:[/*{
//                     name:'after-loan-allot-index',
//                     query:{
//                         routerType:'all'
//                     },
//                     text:'案件管理'
//                 },{
//                     name:'after-loan-wait',
//                     query:{
//                         routerType:'all'
//                     },
//                     text:'全部待催收'
//                 },
//                 {
//                     name:'after-loan-history',
//                     query:{
//                         routerType:'all'
//                     },
//                     text:'全部历史催收'
//                 },{
//                     name:'after-loan-error',
//                     query:{
//                         routerType:'all'
//                     },
//                     text:'全部异常催收'
//                 }
//                 ],
//             }])
//             return
//         }*/
//         if(value == 'operation'){
//             newarr.push([{
//                 text:'运营管理',
//                 router:'operate',
//                 opened:false,
//                 subRouters:[/*{
//                     name:'operate-user',
//                     text:'运营人员管理'
//                 },{
//                     name:'operate-auto-assign',
//                     text:'自动分配管理'
//                 },*/{
//                     name:'operate-assign-records',
//                     text:'认证分配记录'
//                 },{
//                     name:'operate-assign-records-loan',
//                     text:'借款分配记录'
//                 }],
//             }])
//             return
//         }
       
        
//     })
//     commit(types.SET_MENULIST, newarr)
//     Storage.set(`51SjdAdmin_menuList`,newarr)
//     Storage.set(`51SjdAdmin_menuArr`,arr)
// }
//zhixunnaisi
export const setMenuList = ({ commit, state },arr) => {
    
    let newarr = []
    if(!arr){
        arr = Storage.get(`51SjdAdmin_menuArr`) || []
    }
    _.each(arr,(value,index)=>{
        
        if(value == "channel"){
            if(localStorage.getItem("roles")=="channel_operator"){
                    newarr.push([{
                        text:'渠道查询',
                        router:'channel-inquiry',
                        opened:false,
                        subRouters:[/*{
                            name:'channel-management',
                            text:'渠道商管理'
                        },*/{
                            name:'channel-inquiry',
                            text:'渠道查询'
                        }],
                    }])
            }else{
                newarr.push([{
                text:'渠道管理',
                router:'channel',
                opened:false,
                subRouters:[{
                    name:'channel-management',
                    text:'渠道商管理'
                },{
                    name:'channel-inquiry',
                    text:'渠道查询'
                }],
            }])
            }
            
        }
        if(value == "dashboard"){ //
            newarr.push([{
                text:'仪表盘',
                router:'dash-board',
                opened:false,
                 subRouters:[{
                    name:'dash-board',
                    text:'仪表盘'
                }],
            }])
            
           
            return
        }
        if(value == 'financial'){
           
             newarr.push([{
                text:'财务统计报表',
                router:'financial-body',
                opened:false,
                 subRouters:[{
                    name:'financial',
                    text:'财务统计报表'
                }],
            }])
            return
        }
        /*if(value == 'receipt'){
            newarr.push([{
                text:'应收账款统计',
                router:'receipt',
                opened:false,
                 subRouters:[{
                    name:'receipt',
                    text:'应收账款统计'
                }],
            }])
            return
        }*/
      if(value == 'system'){
            newarr.push([{
                text:'通用管理',
                router:'system',
                opened:false,
                subRouters:[{
                    name:'system-user',
                    text:'员工账号管理'
                },{
                    name:'system-product-config',
                    text:'产品配置'
                },{
                    name:'system-risk-config',
                    text:'风控条件配置'
                }/*,{
                    name:'system-icloud-show',
                    text:'iCloud信息配置'
                },{
                    name:'system-app-config',
                    text:'代理商配置'
                }*/],
            }])
            return
        }
        if(value == 'icould'){
            newarr.push([{
                text:'iCloud账号管理',
                router:'icloud',
                opened:false,
                subRouters:[{
                    name:'icloud-index',
                    text:'iCloud账号管理'
                }],
            }])
            return
        }
        if(value == 'auth'){
            newarr.push([{
                text:'认证申请管理',
                router:'auth-apply',
                opened:false,
                subRouters:[{
                    name:'auth-apply-wait',
                    text:'待审核'
                },{
                    name:'auth-apply-history',
                    text:'历史审核'
                }],
            }])
            return
        }
        if(value == 'loan'){
            newarr.push([{
                text:'借款申请管理',
                router:'loan-apply',
                opened:false,
                subRouters:[{
                    name:'loan-apply-wait',
                    text:'待审核'
                },{
                    name:'loan-apply-history',
                    text:'历史审核'
                }/*,{
                    name:'finance-out',
                    query:{
                        type:'other'
                    },
                    text:'放款记录查询'
                }*/],
            }])
            return
        }
        if(value == 'lend'){
            newarr.push([{
                text:'财务管理',
                router:'finance',
                opened:false,
                subRouters:[{
                    name:'finance-wait',
                    text:'待放款记录'
                }/*,{
                    name:'finance-reject',
                    text:'拒绝放款记录'
                }*//*,{
                    name:'finance-ing',
                    text:'放款中记录'
                }*/,{
                    name:'finance-out',
                    text:'放款记录'
                },{
                    name:'receipt',
                    text:'应收账款统计'
                },{
                    name:'finance-extend',
                    text:'展期记录'
                }/*,{
                    name:'customer-management',
                    text:'客户管理'
                }*/],
            }])
            return
        }
        if(value == 'repay'){
            newarr.push([{
                text:'贷后管理',
                router:'after-loan',
                opened:false,
                subRouters:[{
                    name:'after-loan-wait',
                    text:'案件催收' //待催收
                },{
                    name:'after-loan-history',
                    text:'个人对账列表' //历史催收
                },/*{
                    name:'after-loan-error',
                    text:'异常催收'
                },*//*,{
                    name:'finance-out',
                    query:{
                        type:'other'
                    },
                    text:'放款记录查询'
                }*/{
                    name:'after-loan-allot-index',
                    query:{
                        routerType:'all'
                    },
                    text:'案件管理'
                }],
            }])
            return
        }
       /* if(value == 'repay_allot'){
            newarr.push([{
                text:'贷后数据看板', //贷后分配
                router:'after-loan-allot',
                opened:false,
                subRouters:[/*{
                    name:'after-loan-allot-index',
                    query:{
                        routerType:'all'
                    },
                    text:'案件管理'
                },{
                    name:'after-loan-wait',
                    query:{
                        routerType:'all'
                    },
                    text:'全部待催收'
                },
                {
                    name:'after-loan-history',
                    query:{
                        routerType:'all'
                    },
                    text:'全部历史催收'
                },{
                    name:'after-loan-error',
                    query:{
                        routerType:'all'
                    },
                    text:'全部异常催收'
                }
                ],
            }])
            return
        }*/
        if(value == 'operation'){
            newarr.push([{
                text:'运营管理',
                router:'operate',
                opened:false,
                subRouters:[/*{
                    name:'operate-user',
                    text:'运营人员管理'
                },{
                    name:'operate-auto-assign',
                    text:'自动分配管理'
                },*/{
                    name:'operate-assign-records',
                    text:'认证分配记录'
                },{
                    name:'operate-assign-records-loan',
                    text:'借款分配记录'
                }],
            }])
            return
        }
    })
    commit(types.SET_MENULIST, newarr)
    //console.log(newarr)
    Storage.set(`51SjdAdmin_menuList`,newarr)
    Storage.set(`51SjdAdmin_menuArr`,arr)
}






// setSearchHistoryCurrentPage
export const setLoanIndexRefresh = ({ commit, state },boolean ) => {
    commit(types.SET_LOANINDEXREFRESH, boolean)
}

// setAppConfig
export const setAppConfig = ({ commit, state },object ) => {
    commit(types.SET_APPCONFIG, object)
}

// setSessionId
export const setChannel = ({ commit, state }, channel ) => {
	commit(types.SET_CHANNEL, channel)
    Storage.set('channel',channel)
}

// setAdmin
export const setAdmin = ({ commit, state }, admin ) => {
	commit(types.SET_ADMIN, admin)
    Storage.set('admin',admin)
}

// setSessionId
export const setSessionId = ({ commit, state }, sessionId ) => {
    let channel = state.localInfo.channel
    commit(types.SET_SESSIONID, sessionId)
    Storage.set(`51SjdAdmin_sessionId`,sessionId)
    $.extend($.ajaxSettings, {
        headers: {
            'channel': channel,
            'X-Requested-With':'XMLHttpRequest',
            'sessionId':sessionId || ''
        }
    })
}
export const setUserInfo = ({ commit, state }, name ) => {
    commit(types.SET_USERINFO, name)
    Storage.set(`51SjdAdmin_userinfo`,name)
}

export const setRouterList = ({ commit, state }, routerItem) => commit(types.SET_ROUTERLIST, routerItem)
export const setRouterAnimation = ({ commit, state }, animationName ) => commit(types.SET_ROUTERANIMATION, animationName)

export const setSelectZIndex = ({ commit, state }, num ) => commit(types.SET_SELECTZINDEX, num)

export const setDocumentClick = ({ commit, state }, boolean ) => commit(types.SET_DOCUMENTCLICK, boolean)

export const setNeedLogin = ({ commit, state }, boolean ) => commit(types.SET_NEEDLOGIN, boolean)

export const setCity = ({ commit, state }, cityInfo ) => commit(types.SET_CITY, cityInfo)
export const setCarInfo = ({ commit, state }, info ) => commit(types.SET_CARINFO, info)
export const setCarCurrent = ({ commit, state }, info ) => commit(types.SET_CARCURRENT, info)
export const setUserId = ({ commit, state }, id) => commit(types.SET_USERID, id)
export const setCardPrice = ({ commit, state }, priceArr) => commit(types.SET_CARDPRICE, priceArr)

export const addCard = ({ commit, state }, card) => commit(types.ADD_CARD, card)
export const editCard = ({ commit, state }, index, card) => commit(types.EDIT_CARD, index, card)
export const deleteCard = ({ commit, state }, card) => commit(types.DELETE_CARD, card)

export const setBaodanresult = ({ commit, state }, result) => commit(types.SET_BAODANRESULT, result)
export const removeBaodanresult = ({ commit, state }, result) => commit(types.REMOVE_BAODANRESULT, result)

export const setIndexViewForm = ({ commit, state }, form) => commit(types.SET_INDEXVIEWFORM, form)

export const setUpdateDashboard = ({ commit, state }, boolean ) => commit(types.SET_UPDATEDASHBOARD, boolean)
export const setDashboard = ({ commit, state }, dashboard ) => commit(types.SET_DASHBOARD, dashboard)

export const setRequestParams = ({ commit, state }, requestparams ) => commit(types.SET_REQUESTPARAMS, requestparams)
export const setAuthType = ({ commit, state }, type ) => commit(types.SET_AUTHTYPE, type)


